package idv.arthur.jboss.cli;

import java.util.ArrayList;
import java.util.List;

import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;


public class T01 {

	/**
	 * @param args
	 * @throws Exception<p>
	 * Commented on:2014/4/1 下午5:51:21<br>
	 * Created by:  Arthur
	 */
	public static void main(String[] args) throws Exception {
		
		
		/*String cip = "10.144.136.54";
		int cport = 6999;
		String cid = "Arthur";
		char[] cpwd = "abcd1234!@#$".toCharArray();*/
		
		String cip = "10.144.97.99";
		int cport = 9999;
		String cid = "jboss";
		char[] cpwd = "jboss1234!@#$".toCharArray();
		
		CommandContext cmdCtx = null; 
		ModelControllerClient client = null;
		try {
			cmdCtx = initCommandContext(cip, cport, cid, cpwd);
			client = cmdCtx.getModelControllerClient();
			ModelNode requestNode = cmdCtx.buildRequest(":read-resource(recursive=true)");
			ModelNode result = client.execute(requestNode);
			String serverGroupName = "main-server-group";
			List<ReloadList> fileLists = new ArrayList<ReloadList>();
			List<ModelNode> rs = result.get("result").get("deployment").asList();
			
			System.out.println();
			System.out.println("全部的檔案：");
			for (int i=0;i<rs.size();i++) {
				String name = rs.get(i).get(0).get("name").toString().replaceAll("\"", "");
				final ReloadList file = new ReloadList( serverGroupName );
				file.setDepName( name );
				fileLists.add(file);
				System.out.println( file.getDepName() );
			}
			
			
			System.out.println();
			
			rs = result.get("result").get("server-group").get( serverGroupName ).get("deployment").asList();
			System.out.println(serverGroupName+"中有部署的檔案：");
			for (int i=0;i<rs.size();i++) {
				String name = rs.get(i).get(0).get("name").toString().replaceAll("\"", "");
				boolean isEnabled = Boolean.parseBoolean( rs.get(i).get(0).get("enabled").toString() );
				
				int idx=-1;	// 記錄在fileLists中那一個，若還是-1，則表示不存在
				for(int j=0; j<fileLists.size(); j++ ) {
					if( name.equalsIgnoreCase( fileLists.get(j).getDepName() ) ) {
						idx=j;	//表示存在
					}
				}
				
				if( idx != -1 ) {
					if( isEnabled ) {
						fileLists.get(idx).setEnabled(true);
						fileLists.get(idx).setDesc("");
					} else {	//存在，但沒有被開啟
						fileLists.get(idx).setDesc("此模組並沒有被啟動");
					}
				}
				
				System.out.print( name + "\t" );
				System.out.println( isEnabled );
			}
			
			System.out.println();
			
			for (int i=0;i<fileLists.size();i++) {
				System.out.println("Name:"+fileLists.get(i).getDepName());
				System.out.println("isEnabled:"+fileLists.get(i).isEnabled());
				System.out.println("Desc:"+fileLists.get(i).getDesc() );
				System.out.println();
			}
		} finally {
			if (client != null) { client.close(); }
			if (cmdCtx != null) {
				cmdCtx.disconnectController();
				cmdCtx.terminateSession();
			}
		}
	}
	public static CommandContext initCommandContext(String defaultHost,
			int defaultPort, String username, char[] password)
			throws Exception {
		
		CommandContext cmdCtx = null;
		try {
			
			cmdCtx = CommandContextFactory.getInstance().newCommandContext(defaultHost, defaultPort, username, password);
			cmdCtx.connectController();
			cmdCtx.handle("connect");	
		} catch(Exception e) {
			throw e;
		}
		 

		return cmdCtx;
	}
}

class ReloadList {
	
	private boolean isEnabled=false;	//預設就是不能勾
	private String depName;
	private String desc="此檔不存在於這個Server Group";				//描述不能被勾選的理由
	
	public ReloadList( String serverGroup ) {
		this.desc = "此檔並未分配給"+serverGroup;
	}
	
	public boolean isEnabled() {
		return isEnabled;
	}
	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}
	public String getDepName() {
		return depName;
	}
	public void setDepName(String depName) {
		this.depName = depName;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
}